﻿using System.Globalization;
using EPiServer.Core;
using System.Linq;
using EPiServer.Shell.UI.Models;

namespace OpenWaves.EPiServer.GenericListProperty
{
    public static class PropertyExtensions
    {
        public static readonly string[] StandardContentProperyNames = new[]
            {
                "icontent_name", "icontent_contentlink", "icontent_parentlink", "icontent_contenttypeid",
                "icontent_isdeleted", "icategorizable_category", "iversionable_ispendingpublish",
                "iversionable_startpublish", "iversionable_stoppublish", "iversionable_shortcut", "iversionable_expire",
                "ichangetrackable_created", "ichangetrackable_createdby", "ichangetrackable_changed",
                "ichangetrackable_setchangedonpublish", "ichangetrackable_changedby", "ichangetrackable_saved",
                "ichangetrackable_deletedby", "ichangetrackable_deleted"
            };

        public static bool IsStandardProperty(this PropertyData propertyData)
        {
            return StandardContentProperyNames.Contains(propertyData.Name.ToLowerInvariant());
        }

        public static bool IsStandardProperty(this MetadataStoreModel metadataViewModel)
        {
            return StandardContentProperyNames.Contains(metadataViewModel.Name.ToLowerInvariant());
        }

        public static string ToJavascriptPropertyName(this string propertyName)
        {
            if (string.IsNullOrWhiteSpace(propertyName))
            {
                return propertyName;
            }

            return propertyName[0].ToString(CultureInfo.InvariantCulture).ToLowerInvariant() + propertyName.Substring(1);
        }
    }
}